<template>
  <div class="grammar-check-container">
    <h2 class="text-2xl font-bold mb-6">语法检查与修正</h2>
    
    <div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
      <div>
        <label class="block text-gray-700 font-medium mb-2">输入文本</label>
        <textarea 
          v-model="inputText" 
          rows="20" 
          class="w-full px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
          placeholder="请输入需要检查的文本..."
        ></textarea>
      </div>
      
      <div>
        <label class="block text-gray-700 font-medium mb-2">检查结果</label>
        <div 
          class="w-full h-[480px] p-4 border border-gray-300 rounded-md overflow-auto"
          :class="{'bg-gray-50': !checkedText}"
        >
          <div v-if="!checkedText" class="h-full flex items-center justify-center text-gray-500">
            点击"检查语法"按钮开始检查
          </div>
          <div v-else v-html="highlightCorrections()"></div>
        </div>
      </div>
    </div>
    
    <div class="mt-6 flex justify-between">
      <button 
        @click="clearText" 
        class="bg-gray-200 hover:bg-gray-300 text-gray-800 font-medium py-2 px-4 rounded-md transition-colors"
      >
        清空文本
      </button>
      <button 
        @click="checkGrammar" 
        class="bg-blue-500 hover:bg-blue-600 text-white font-medium py-2 px-4 rounded-md transition-colors"
        :disabled="!inputText || isLoading"
      >
        {{ isLoading ? '检查中...' : '检查语法' }}
      </button>
    </div>
    
    <div v-if="correctionDetails.length > 0" class="mt-8">
      <h3 class="text-xl font-bold mb-4">修正详情</h3>
      <div class="space-y-4">
        <div 
          v-for="(correction, index) in correctionDetails" 
          :key="index"
          class="p-4 border border-gray-200 rounded-lg"
        >
          <div class="flex justify-between items-start">
            <div>
              <p class="font-medium text-gray-900">问题 {{ index + 1 }}</p>
              <p class="text-sm text-gray-500 mt-1">{{ correction.error_type }}</p>
            </div>
            <span class="px-2 py-1 bg-red-100 text-red-800 text-xs rounded-full">
              {{ correction.severity }}
            </span>
          </div>
          <div class="mt-3">
            <p class="text-sm text-gray-700">
              <span class="font-medium">原文:</span> {{ correction.original_text }}
            </p>
            <p class="text-sm text-green-700 mt-1">
              <span class="font-medium">修正建议:</span> {{ correction.suggestion }}
            </p>
            <p class="text-xs text-gray-500 mt-2">
              <span class="font-medium">解释:</span> {{ correction.explanation }}
            </p>
          </div>
        </div>
      </div>
    </div>
    
    <div v-if="errorMessage" class="mt-4 p-4 bg-red-100 border border-red-400 text-red-700 rounded-md">
      {{ errorMessage }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      inputText: '',
      checkedText: '',
      correctionDetails: [],
      isLoading: false,
      errorMessage: ''
    }
  },
  methods: {
    clearText() {
      this.inputText = '';
      this.checkedText = '';
      this.correctionDetails = [];
      this.errorMessage = '';
    },
    async checkGrammar() {
      if (!this.inputText) return;
      
      this.isLoading = true;
      this.checkedText = '';
      this.correctionDetails = [];
      this.errorMessage = '';
      
      try {
        const response = await this.$axios.post('/api/grammar/check', {
          text: this.inputText
        });
        
        this.checkedText = response.data.corrected_text;
        this.correctionDetails = response.data.details;
      } catch (error) {
        this.errorMessage = error.response?.data?.error || '语法检查失败，请稍后再试';
        console.error(error);
      } finally {
        this.isLoading = false;
      }
    },
    highlightCorrections() {
      if (!this.checkedText || !this.correctionDetails.length) return this.checkedText;
      
      let highlightedText = this.checkedText;
      
      // 按位置倒序排列修正项，避免替换后位置变化
      const sortedCorrections = [...this.correctionDetails].sort((a, b) => b.start - a.start);
      
      sortedCorrections.forEach(correction => {
        const original = this.checkedText.substring(correction.start, correction.end);
        const replacement = `<span class="bg-yellow-200 underline decoration-red-500">${original}</span>`;
        highlightedText = highlightedText.substring(0, correction.start) + replacement + highlightedText.substring(correction.end);
      });
      
      return highlightedText;
    }
  }
}
</script>